//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Vasicek.h"
using namespace Cephei::QL::Models::Shortrate::Onefactormodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Models/Shortrate/OneFactorAffineModel.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Models::Shortrate;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (Microsoft::FSharp::Core::FSharpOption<Double>^ r0, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ b, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma, Microsoft::FSharp::Core::FSharpOption<Double>^ lambda) : COneFactorAffineModel(CVasicek::typeid)
{
    try
    {
#ifdef HANDLE
        _phVasicek = NULL;
#endif
        QuantLib::Rate _r0 = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (r0) ? (QuantLib::Rate)ValueHelper::Convert (r0->Value) : 0.05); //4
        QuantLib::Real _a = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (a) ? (QuantLib::Real)ValueHelper::Convert (a->Value) : 0.1); //4
        QuantLib::Real _b = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (b) ? (QuantLib::Real)ValueHelper::Convert (b->Value) : 0.05); //4
        QuantLib::Real _sigma = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (sigma) ? (QuantLib::Real)ValueHelper::Convert (sigma->Value) : 0.01); //4
        QuantLib::Real _lambda = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (lambda) ? (QuantLib::Real)ValueHelper::Convert (lambda->Value) : 0.0); //4
        _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (new QuantLib::Vasicek ( _r0,  _a,  _b,  _sigma,  _lambda ));
        SetOneFactorAffineModel (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (boost::shared_ptr<QuantLib::Vasicek>& childNative, Object^ owner) : COneFactorAffineModel(CVasicek::typeid)
{
#ifdef HANDLE
	_phVasicek = NULL;
#endif
	_ppVasicek = &childNative;
    _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
}
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (QuantLib::Vasicek& childNative, Object^ owner) : COneFactorAffineModel(CVasicek::typeid)
{
#ifdef HANDLE
	_phVasicek = NULL;
#endif
	_ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (&childNative);
    _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
    _VasicekOwner = owner;
    _OneFactorAffineModelOwner = owner;
}

Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (CVasicek^ copy) : COneFactorAffineModel(CVasicek::typeid)
{
#ifdef HANDLE
	_phVasicek = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (copy->GetShared());
        _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (System::Type^ t) : COneFactorAffineModel(CVasicek::typeid)
{
#ifdef HANDLE
	_phVasicek = NULL;
#endif
	if (!t->IsSubclassOf(CVasicek::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (QuantLib::Handle<QuantLib::Vasicek>& childNative, Object^ owner)  : COneFactorAffineModel(CVasicek::typeid)
{
	_phVasicek = &childNative;
	_ppVasicek = &static_cast<boost::shared_ptr<QuantLib::Vasicek>>(childNative.currentLink());
    _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
    _VasicekOwner = owner;
}
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (QuantLib::Handle<QuantLib::Vasicek> childNative)  : COneFactorAffineModel(CVasicek::typeid)
{
	_phVasicek = &childNative;
	_ppVasicek = &static_cast<boost::shared_ptr<QuantLib::Vasicek>>(childNative.currentLink());
    _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::CVasicek (QuantLib::Vasicek childNative)  : COneFactorAffineModel(CVasicek::typeid)
{
#ifdef HANDLE
	_phVasicek = NULL;
#endif
	_ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (new QuantLib::Vasicek (childNative));
    _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (boost::dynamic_pointer_cast<QuantLib::OneFactorAffineModel> (*_ppVasicek));
}
#endif

Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::~CVasicek ()
{
    if (_ppVasicek != NULL)
    {
	    delete _ppVasicek;
        _ppVasicek = NULL;
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::!CVasicek ()
{
    if (_ppVasicek != NULL)
    {
	    delete _ppVasicek;
    }
}
QuantLib::Vasicek& Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::GetReference ()
{
    if (_ppVasicek == NULL) throw gcnew NativeNullException ();
	return **_ppVasicek;
}
boost::shared_ptr<QuantLib::Vasicek>& Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::GetShared ()
{
    if (_ppVasicek == NULL) throw gcnew NativeNullException ();
	return *_ppVasicek;
}
QuantLib::Vasicek* Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::GetPointer ()
{
    if (_ppVasicek == NULL) throw gcnew NativeNullException ();
	return &**_ppVasicek;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Vasicek>& Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::GetHandle ()
{
	if (_phVasicek == NULL)
	{
		_phVasicek = new Handle<QuantLib::Vasicek> (*_ppVasicek);
	}
	return *_phVasicek;
}
#endif
bool Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::HasNative () 
{
	return (_ppVasicek != NULL);
}

Double Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek::DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity)
{
    try
    {
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        QuantLib::Time _bondMaturity = (QuantLib::Time)ValueHelper::Convert (bondMaturity);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVasicek)->discountBondOption ( _type,  _strike,  _maturity,  _bondMaturity );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Shortrate::Onefactormodels::IVasicek^ Cephei::QL::Models::Shortrate::Onefactormodels::CVasicek_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ r0, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ b, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma, Microsoft::FSharp::Core::FSharpOption<Double>^ lambda)
{
    return gcnew CVasicek ( r0,  a,  b,  sigma,  lambda);
}
